package com.sdkx.wear_os.utils

import java.math.RoundingMode
import kotlin.math.abs

/**
 * 度分转经纬度
 * @param dmStr 4005.38389 ddmm.mmmmm
 * @return 40.0897315
 */
fun dfToLatLng(dmStr: String?): Double {
    var dm = dmStr ?: return 0.0
    try {
        dm = dm.replace(" ", "")
        val d = parseInteger(dm.substring(0, dm.lastIndexOf(".") - 2))
        // 兼容经纬度的转换
        val fen = dm.substring(d.toString().length).toDouble()
        var lat = fen / 60 + abs(d)
        if (lat < 0) {
            lat = -lat
        }
        return String.format("%.8f", lat).toDouble()
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return 0.0
}

/**
 * 经纬度转度分
 * @param du 40.0897315
 * @return 4005.38389 ddmm.mmmmm
 */
fun latLngToDf(du: Double): Double {
    val du1 = du.toInt()
    val tp = (du - du1) * 60
    val fen = NumberUtils.toDouble(tp.toString() + "", 5, RoundingMode.HALF_UP)
    return java.lang.Double.valueOf(du1.toString() + "" + if (fen < 10) "0$fen" else fen)
}

fun parseInteger(str: String?): Int {
    return if (str == null || str.isEmpty()) {
        0
    } else Integer.valueOf(str)
}